package com.example.im.mvp.presenter

import android.content.Context
import com.example.im.entity.FriendOrRoom
import com.example.im.mvp.base.BasePresenter
import com.example.im.mvp.view.IUserView
import com.example.im.xmpp.IMConnection
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.BiFunction
import io.reactivex.schedulers.Schedulers

class UserPresenter(mContext: Context, mView: IUserView?) :
    BasePresenter<IUserView>(mContext, mView) {

    private var imConnection: IMConnection? = null

    init {
        imConnection = IMConnection.instance!!
    }

    override fun detachView() {
        super.detachView()
        this.disposable.clear()
    }

    /**
     * 搜索用户
     * */
    fun userSearch(userId: String) {
        if (userId.isEmpty()) {
            getView().onHint("请输入您要搜索的ID")
            return
        }

        val observable = Observable.just(userId)
            .subscribeOn(Schedulers.io())
            .map {
                imConnection!!.userSearch(it)
            }
            .observeOn(AndroidSchedulers.mainThread())
            .filter {
                if (it.isEmpty())
                    getView().onHint("未找到该用户ID")
                it.isNotEmpty()
            }
            .flatMap {
                Observable.fromIterable(it)
            }
            .map {
                with(FriendOrRoom(), {
                    this.jid = it.getValues("jid")[0].toString()
                    this.account = it.getValues("Username")[0].toString()
                    this.nickname = it.getValues("Name")[0].toString()
                    this.getFriendOrRoom()
                })
            }
            .toList()
            .toObservable()
            .subscribe(
                { getView().onSearchFriendGood(it) },
                { getView().onHint(it.message!!) }
            )
        this.disposable.add(observable)
    }

    /**
     * 添加用户
     * */
    fun userAddition(userName: String, name: String) {
        if (userName.isEmpty()) {
            getView().onHint("用户账号不能为空")
            return
        }
        if (name.isEmpty()) {
            getView().onHint("用户昵称不能为空")
            return
        }

        val observable = Observable.zip(
            Observable.just(userName),
            Observable.just(name),
            BiFunction<String, String, Boolean> { t1, t2 ->
                imConnection!!.createEntry(t1, t2)
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                {
                    when (it) {
                        false -> getView().onHint("添加失败！")
                        else -> {
                            getView().onHint("已发送添加好友请求,请等待对方确认！")
                            getView().onAdditionSuccess()
                        }
                    }
                },
                { getView().onHint(it.message!!) }
            )
        this.disposable.add(observable)
    }

    /**
     * 删除用户
     * */
    fun userDelete(friend: FriendOrRoom) {
        val observable = Observable.just(friend)
            .subscribeOn(Schedulers.io())
            .map { imConnection!!.removeEntry(friend.jid!!) }
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                {
                    if (!it) getView().onHint("删除用户失败") else {
                        getView().onRemoverSuccess(friend)
                    }
                },
                { getView().onHint(it.message!!) }
            )
        this.disposable.add(observable)
    }

}